﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;
using System.Web;

namespace YouHP.Pages.User
{
    public class FriendAdd : UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (this.ErrorCount > 0) return;

            int uid = RequestTools.GetInt("uid", 0);
            string msg = RequestTools.GetString("msg").Trim();

            if (UserData.Id <= 0)
            {
                ResponseMsg("您还未登录！");
                return;
            }

            if (uid <= 0)
            {
                ResponseMsg("错误的参数 uid");
                return;
            }

            UserInfo info = UserBLL.Instance.GetUserInfo(uid);
            if (info.Id <= 0)
            {
                ResponseMsg("用户不存在");
                return;
            }

            if (uid == UserData.Id)
            {
                ResponseMsg("您不能加自己为好友！");
                return;
            }

            int state = FriendBLL.Instance.CheckFriend(UserData.Id, uid);
            if (state == Constants.FriendPass)
            {
                ResponseMsg(string.Format("{0} 已经是您的好友了", info.UserName));
                return;
            }
            else if (state == Constants.FriendNoPass)
            {
                ResponseMsg(string.Format("抱歉，{0} 已设置忽略您的好友申请！", info.UserName));
                return;
            }
            else if (state == Constants.FriendApplying)
            {
                ResponseMsg(string.Format("您已经向 {0} 提交过好友申请，不要再次提交！ ", info.UserName));
                return;
            }
            else
            {
                FriendInfo finfo = new FriendInfo();
                finfo.FriendUserId = uid;
                finfo.FriendUserName = info.UserName;
                finfo.HostUserId = UserData.Id;
                finfo.HostUserName = UserData.UserName;
                finfo.State = Constants.FriendApplying;
                finfo.PostTime = DateTime.Now;
                finfo.UpdateTime = DateTime.Now;
                finfo.AppMsg = msg;

                bool succ = FriendBLL.Instance.AddFriend(finfo);
                if (succ)
                {
                    ResponseMsg(string.Format("申请成功，请等待 {0} 的同意", info.UserName));
                    return;
                }
                else
                {
                    ResponseMsg("操作失败！");
                    return;
                }

            }
        }

        protected void ResponseMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(msg);
            ResponseXML(sb);
        }


        protected void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }

    }
}